<?php

namespace addons\AppVersions\html5\controllers;

use addons\AppVersions\common\enums\TypeEnum;
use addons\AppVersions\common\models\SettingForm;
use common\enums\StatusEnum;
use addons\AppVersions\common\models\AppVersions;
use common\helpers\AddonHelper;

/**
 * 默认控制器
 *
 * Class DefaultController
 * @package addons\AppVersions\html5\controllers
 */
class DefaultController extends BaseController
{
    /**
    * 首页
    *
    * @return string
    */
    public function actionIndex()
    {
        return $this->render('index',[

        ]);
    }

    /**
     * 下载页面
     *
     * @return string
     */
    public function actionDownload()
    {
        $setting = new SettingForm();
        $setting->attributes = AddonHelper::getConfig();

        return $this->render($this->action->id, [
            'setting' => $setting,
            'android' => $this->findNewest(TypeEnum::ANDROID),
            'ios' => $this->findNewest(TypeEnum::IOS),
        ]);
    }

    /**
     * @param $type
     * @return array|\yii\db\ActiveRecord|null
     */
    protected function findNewest($type)
    {
        return AppVersions::find()
            ->where(['status' => StatusEnum::ENABLED])
            ->andWhere(['type' => $type])
            ->orderBy('id desc')
            ->asArray()
            ->one();
    }
}